package omm.domain.comparativegraph;

import java.util.HashMap;
import java.util.Map;

import omm.domain.CommissionStrategy;

public abstract class GraphicStrategy {

	 public abstract String getGraphicURL(Map<String, Double> data, CommissionStrategy commissionStrategy);

	 protected Map<String, Integer> calculatePercentages(Map<String, Double> data) {
			Map<String, Integer> percentages = new HashMap<String, Integer>();
			Double total = 0.0;
			for (Map.Entry<String, Double> e : data.entrySet()) {
				total += e.getValue();
			}

			Integer othersPercent = 100;
			for (Map.Entry<String, Double> e : data.entrySet()) {
				Integer percent = (int) Math.round(e.getValue() * 100 / total);
				if (percent > 1) {
					percentages.put(e.getKey(), percent);
					othersPercent -= percent;
				}
			}

			if (othersPercent > 0)
				percentages.put("OTROS", othersPercent);

			return percentages;
		}

	 protected String percentagesToString(Map<String, Integer> percentages) {
			String percentagesStr = "";
			for (Integer p : percentages.values()) {
				percentagesStr = percentagesStr + p + ',';
			}

			return percentagesStr.substring(0, percentagesStr.length() - 1);
		}
	 
}
